package XynLeen.GraduationProject;

import android.os.Build;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;


import com.youth.xframe.XFrame;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.concurrent.TimeUnit;

import XynLeen.GraduationProject.BackPress.BaseActivity;
import XynLeen.GraduationProject.Bean.UserBean;
import XynLeen.GraduationProject.Fragments.Personal.FragmentC;
import XynLeen.GraduationProject.Fragments.Index.FragmentA;
import XynLeen.GraduationProject.Fragments.Tools.FragmentB;
import okhttp3.OkHttpClient;


public class MainActivity extends BaseActivity {

    private BottomNavigationView mBottomNaView;
    private FragmentA fragmenta;
    private FragmentB fragmentb;
    private FragmentC fragmentc;
    private Fragment[] fragments;
    private int lastShowFragment = 0;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.item_1:
                    if (lastShowFragment != 0) {
                        switchFrament(lastShowFragment, 0);
                        lastShowFragment = 0;
                    }
                    return true;
                case R.id.item_2:
                    if (lastShowFragment != 1) {
                        switchFrament(lastShowFragment, 1);
                        lastShowFragment = 1;
                    }
                    return true;
                case R.id.item_3:
                    if (lastShowFragment != 2) {
                        switchFrament(lastShowFragment, 2);
                        lastShowFragment = 2;
                    }
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navigation = findViewById(R.id.bottom_view);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        initFragments();

        //配置
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(100000L, TimeUnit.MILLISECONDS)
                .readTimeout(100000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
        //android 7.0 调用系统相机崩溃
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

        XFrame.init(this);
        //初始化日志
        XFrame.initXLog();
        //初始化多状态界面View
        XFrame.initXLoadingView()
                .setErrorViewResId(R.layout._loading_layout_error);
    }

    public void switchFrament(int lastIndex, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //将fragment加入回退栈
        //transaction.addToBackStack(null);
        transaction.hide(fragments[lastIndex]);
        if (!fragments[index].isAdded()) {
            transaction.add(R.id.fragment_container, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

    private void initFragments() {
        fragmenta = new FragmentA();
        fragmentb = new FragmentB();
        fragmentc = new FragmentC();
        fragments = new Fragment[]{fragmenta, fragmentb, fragmentc};
        lastShowFragment = 0;
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, fragmenta)
                .show(fragmenta)
                .commit();
    }


}
